package org.cable.shop.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.cable.common.enums.BizCodeEnum;
import org.cable.common.exception.BizException;
import org.cable.common.model.EventMessage;
import org.cable.shop.service.ProductOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 关闭订单接口
 *
 * @author hsj
 * @date 2023/11/8 17:06
 */
@Component
@Slf4j
@RabbitListener(queuesToDeclare = {
			@Queue("order.close.queue"),
			@Queue("order.update.queue")
})
public class ProductOrderMQListener {

	@Autowired
	private ProductOrderService productOrderService;

	@RabbitHandler
	public void productOrderHandler(EventMessage eventMessage, Message message, Channel channel) {

		log.info("监听到消息ProductOrderMQListener的消息内容是:{}",message);

        try {
			productOrderService.handleProductOrderMessage(eventMessage);
        }catch (Exception e){
			log.error("消费者失败:{}",eventMessage);
			throw new BizException(BizCodeEnum.MQ_CONSUMER_EXCEPTION);
        }

		log.info("消费成功:{}",eventMessage);

	}


}
